#添加头文件和库文件的搜索路径 RYCMAKE_DIRECTORIES_THRID为目录名
#如需使用Qt设置RYCMAKE_QT_LIBRARIES
macro(rycmake_add_directories)
	foreach(DIRECTORY_THRIDPARTY ${RYCMAKE_DIRECTORIES_THRID})
		if(${DIRECTORY_THRIDPARTY} MATCHES "Qt" )
			set(Qt5_DIR ${PROJECT_ROOT_DIR}/thrid_party/${DIRECTORY_THRIDPARTY})
			set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${Qt5_DIR})
			set(CMAKE_AUTOMOC ON)
			set(CMAKE_AUTOUIC ON)
			find_package(Qt5 REQUIRED ${RYCMAKE_QT_LIBRARIES})
			message(STATUS "${PROJECT_NAME} 添加Qt5::Dir 库目录")
		else()
			if(${DIRECTORY_THRIDPARTY} MATCHES "GRPC")
				add_definitions(-D_WIN32_WINNT=0x0A00)
			endif()
			include_directories(${PROJECT_ROOT_DIR}/thrid_party/${DIRECTORY_THRIDPARTY}/include)
			link_directories(${PROJECT_ROOT_DIR}/thrid_party/${DIRECTORY_THRIDPARTY}/lib)
			message(STATUS "${PROJECT_NAME} 添加  ${DIRECTORY_THRIDPARTY} 索引目录")
		endif()
	endforeach()
endmacro(rycmake_add_directories)

macro(rycmake_target_link_other) #连接其他库，填充RYCMAKE_OTHER_LINKS为库的名字
	foreach(LINK_DIR ${RYCMAKE_OTHER_LINKS})
		target_link_libraries(${PROJECT_NAME} ${LINK_DIR})
		message(STATUS "${PROJECT_NAME} link ${LINK_DIR}")
	endforeach()
endmacro(rycmake_target_link_other)

macro(rycmake_target_link_qt) #链接QT库
	foreach(QC_LINK ${RYCMAKE_QT_LIBRARIES})
		target_link_libraries(${PROJECT_NAME} Qt5::${QC_LINK})
		message(STATUS "${PROJECT_NAME} link Qt5::${QC_LINK}")
	endforeach()
endmacro(rycmake_target_link_qt)

macro(rycmake_platform_init type3 type4) #type3 设置C++标准 type4设置输出路径

	if(NOT ${type3})
		set(CMAKE_CXX_STANDARD 14)
		message(STATUS "${PROJECT_NAME} 设置C++14标准")
	else()
		set(CMAKE_CXX_STANDARD ${type3})
		message(STATUS "${PROJECT_NAME} 设置C++${type3}标准")
	endif()
	if(${type4} MATCHES 0)
		message(STATUS "${PROJECT_NAME} 默认编译输出路径")	
	else()
		if(${CMAKE_SYSTEM_NAME} MATCHES Windows)
			set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${type4}/lib)
			set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${type4}/lib)
			set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${type4}/debug)
			set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${type4}/release)
			message(STATUS "${PROJECT_NAME} 设置编译输出路径")
		elseif(${CMAKE_SYSTEM_NAME} MATCHES Linux)
			message(STATUS "${PROJECT_NAME} 设置编译输出路径Linux未知")
		else()
			message("未知平台，输出路径未设置")
		endif()
	endif()
endmacro(rycmake_platform_init)

macro(rycmake_create_project type1 type2) #指定生成类型  type1生成exe还是lib  type2设置编译版本
	message(STATUS "${PROJECT_NAME} 开始搭建环境")
	rycmake_add_directories() #添加库索引路径
	if(CMAKE_GENERATOR MATCHES "Visual Studio")
		if(${type2} MATCHES Release)
			set(CMAKE_BUILD_TYPE Release)
		 #Release调试设置
			set(CMAKE_CXX_FLAGS_RELEASE "/Zi")	
			message(STATUS "${PROJECT_NAME} 设置优化/zi")
		endif()
	endif()
		
	file(GLOB_RECURSE HEADER_FILES "*.h" "*.hpp" "*.inc")
	file(GLOB_RECURSE SOURCE_FILES "*.c" "*.cpp" "*.cxx" "*.qrc" "*.rc")
	file(GLOB_RECURSE UI_FILES "*.ui")
	
	set(RYCMAKE_OTHER_HEADER) #设置上层用户可包裹的路径
	set(RYCMAKE_OTHER_UI)
	set(RYCMAKE_OTHER_SOURCE)
	set(RYCMAKE_MSG "正在添加上层包含路径 .")
	foreach(RYCMAKE_DIRPATH ${RYCMAKE_DIRPATHS})
		file(GLOB RYCMAKE_OTHER_HEADER_X "${RYCMAKE_DIRPATH}/*.h" "${RYCMAKE_DIRPATH}/*.hpp" "${RYCMAKE_DIRPATH}/*.inc")
		file(GLOB RYCMAKE_OTHER_SOURCE_Y "${RYCMAKE_DIRPATH}/*.c" "${RYCMAKE_DIRPATH}/*.cpp" "${RYCMAKE_DIRPATH}/*.cxx" "${RYCMAKE_DIRPATH}/*.qrc" "${RYCMAKE_DIRPATH}/*.rc")
		file(GLOB RYCMAKE_OTHER_UI_Z "${RYCMAKE_DIRPATH}/*.ui")
		
		list(APPEND RYCMAKE_OTHER_HEADER ${RYCMAKE_OTHER_HEADER_X})
		list(APPEND RYCMAKE_OTHER_SOURCE ${RYCMAKE_OTHER_SOURCE_Y})
		list(APPEND RYCMAKE_OTHER_UI ${RYCMAKE_OTHER_UI_Z})
		
		include_directories(${RYCMAKE_DIRPATH})		
		message(STATUS ${RYCMAKE_MSG})
		list(APPEND RYCMAKE_MSG ".")
	endforeach()
	
	set(RYCMAKE_GLOBAL_FILE_HEADS)
	set(RYCMAKE_GLOBAL_FILE_SOURCES)
	set(RYCMAKE_GLOBAL_FILE_UI)

	if(RYCMAKE_GLOB_DIRPATHS)
		file(GLOB_RECURSE RYCMAKE_GLOBAL_FILE_HEADS "${RYCMAKE_GLOB_DIRPATHS}/*.h" "${RYCMAKE_GLOB_DIRPATHS}/*.hpp" "${RYCMAKE_GLOB_DIRPATHS}/*.inc")
		file(GLOB_RECURSE RYCMAKE_GLOBAL_FILE_SOURCES "${RYCMAKE_GLOB_DIRPATHS}/*.c" "${RYCMAKE_GLOB_DIRPATHS}/*.cpp" "${RYCMAKE_GLOB_DIRPATHS}/*.cxx" "${RYCMAKE_GLOB_DIRPATHS}/*.qrc" "*.rc")
		file(GLOB_RECURSE RYCMAKE_GLOBAL_FILE_UI "${RYCMAKE_GLOB_DIRPATHS}/*.ui")
		include_directories(${RYCMAKE_GLOB_DIRPATHS})
		message("${PROJECT_NAME} 添加头文件包含路径${RYCMAKE_GLOB_DIRPATHS}")
	endif()

	IF(RYCMAKE_DIRPATHS)
		list(APPEND RYCMAKE_MSG "  OK")	
		message(STATUS ${RYCMAKE_MSG})
	ENDIF()
	
	IF(REMOVE_FILES_LIST)
		list(REMOVE_ITEM SOURCE_FILES ${REMOVE_FILES_LIST})
	ENDIF()
	source_group("Header Files" FILES ${HEADER_FILES} ${RYCMAKE_OTHER_HEADER} ${RYCMAKE_GLOBAL_FILE_HEADS})
	source_group("Ui Files" FILES ${UI_FILES} ${RYCMAKE_OTHER_UI} ${RYCMAKE_GLOBAL_FILE_UI})
	source_group("Source Files" FILES ${SOURCE_FILES} ${RYCMAKE_OTHER_SOURCE} ${RYCMAKE_GLOBAL_FILE_SOURCES})

	if(${type1} MATCHES exe)
		add_executable(${PROJECT_NAME} ${HEADER_FILES} ${UI_FILES} ${SOURCE_FILES} ${RYCMAKE_OTHER_HEADER} ${RYCMAKE_OTHER_UI} ${RYCMAKE_OTHER_SOURCE}  
		${RYCMAKE_GLOBAL_FILE_HEADS} ${RYCMAKE_GLOBAL_FILE_SOURCES}  ${RYCMAKE_GLOBAL_FILE_UI}
		)
		message(STATUS "${PROJECT_NAME} 将生成可执行文件 工程")
		
	else()
		if(${type1} MATCHES STATIC)
			add_library(${PROJECT_NAME} STATIC  ${HEADER_FILES} ${UI_FILES} ${SOURCE_FILES} ${RYCMAKE_OTHER_HEADER} 
			${RYCMAKE_OTHER_UI} ${RYCMAKE_OTHER_SOURCE}  ${RYCMAKE_GLOBAL_FILE_HEADS} 
			${RYCMAKE_GLOBAL_FILE_SOURCES}
			)
		elseif(${type1} MATCHES SHARED)
			add_library(${PROJECT_NAME} SHARED  ${HEADER_FILES} ${UI_FILES} ${SOURCE_FILES} 
			${RYCMAKE_OTHER_HEADER} ${RYCMAKE_OTHER_UI} ${RYCMAKE_OTHER_SOURCE}  
			${RYCMAKE_GLOBAL_FILE_HEADS} ${RYCMAKE_GLOBAL_FILE_SOURCES}  ${RYCMAKE_GLOBAL_FILE_UI}
			)
		else()
			add_library(${PROJECT_NAME} SHARED  ${HEADER_FILES} ${UI_FILES} ${SOURCE_FILES} 
			${RYCMAKE_OTHER_HEADER} ${RYCMAKE_OTHER_UI} ${RYCMAKE_OTHER_SOURCE} 
			${RYCMAKE_GLOBAL_FILE_HEADS} ${RYCMAKE_GLOBAL_FILE_SOURCES}  ${RYCMAKE_GLOBAL_FILE_UI}
			 ) 
		endif()
		message(STATUS "${PROJECT_NAME} 将生成 ${type1} 库")
	endif()
	rycmake_target_link_qt()#连接Qt
	rycmake_target_link_other()#链接除Qt外其他库
	if(MSVC)
		if(${type2} MATCHES Release)
			set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_RELEASE "/DEBUG")
			message(STATUS "${PROJECT_NAME} 开启 Release调试")
		endif()
	endif(MSVC)
	message(STATUS "${PROJECT_NAME} 搭建环境完毕")
endmacro(rycmake_create_project)


